<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\db;

class Index extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $request = Request::instance();
        $controller = $request->controller(); // 获取访问的控制器名
        $action = $request->action(); // 获取访问的方法名
        $wantnode = $controller.'-'.$action;
//        $a = input('session.');
////        dump($a);
        // 通过接口获得平台发送的消息
        $url = 'http://www.miaozy.cn/news?token=3116b736c0a7e0830d38102d5ee140cc';
        $msg = https_request($url);
        $msg = json_decode($msg,true);
        session('wdcount',$msg['wdcount']);

        // 查询待实名审核数量并存入session
        $shimingshenhecount = db('user_data')
            ->where('smstatus',2)
            ->count('uid');
        $shimingshenhecount = empty($shimingshenhecount)?'0':$shimingshenhecount;
        if ($shimingshenhecount != session('info.shimingshenhecount')){
            session('info.shimingshenhecount',$shimingshenhecount);
        }

        // 查询待回复的询价数量并存入session
        $askcount = db('ask')
            ->where('status',1)
            ->count('id');
        $askcount = empty($askcount)?'0':$askcount;
        if ($askcount != session('info.askcount')){
            session('info.askcount',$askcount);
        }

        // 查询正常用户数量并存入session
        $usercount = db('user')
            ->where('status',1)
            ->count('id');
        $usercount = empty($usercount)?'0':$usercount;
        if ($usercount != session('info.usercount')){
            session('info.usercount',$usercount);
        }

        // 查询1天内交易数量并存入session
        $ordercount = db('order')
            ->where('selltime','between',[strtotime('today-1day'),time()])
            ->count('id');
        $ordercount = empty($ordercount)?'0':$ordercount;
        if ($ordercount != session('info.ordercount')){
            session('info.ordercount',$ordercount);
        }

//        dump(session('node.mname'));
//
//        dump(session('node'));
//        dump(session('username'));


        // 权限控制
        if(!session('?username')){
            $this->error('你还未登录!', url('admin/Index/login'),'',2);
        }else{
            if(session('username') != 'admin') {
                $node = session('node');
//
                if (!in_array(strtolower($wantnode), $node)) {
                    $this->error('您没有访问权限');
                }
            }
        }
        return view();
    }

    public function login()
    {
        return view();
    }

    public function dologin()
    {
        $data = input('post.');
        $res = db('admin_user')
            ->where([
                'username'=>$data['name'],
                'userpass'=>md5($data['pwd'])
            ])
            ->find();
//        dump($res);

        if(!empty($res)){
            //判断用户是否激活
            if($res['status'] != 1 && $res['username'] != 'admin'){
                $this->error('您的帐号已被禁用');
            }

            session(null);
            $username = $res['username'];
            $userid = $res['id'];

            $userinfo = Db::view(['qq_node'=>'qn'], ['id'=>'nodeid','name','mname','aname','status'=>'nodestatus'])
                ->view(['qq_role_node'=>'qrn'], ['nid'], 'qn.id = qrn.nid')
                ->view(['qq_role'=>'qr'], ['id'], 'qrn.rid = qr.id')
                ->view(['qq_user_role'=>'qur'], ['rid'], 'qr.id = qur.rid')
                ->view(['qq_admin_user'=>'qu'], ['id'], 'qur.uid = qu.id')
                ->where([
                    'qu.id'=>['=',$res['id']],
                    'qu.status&qr.status&qn.status'=>['=',1]
                    ])
                ->select();
            session('userid',$userid);
            session('username',$username);
//            dump($userinfo);
            $nodename = array();
            foreach($userinfo as $k => $v){
                $nodename[] = strtolower($v['mname']).'-'.strtolower($v['aname']);
            }
//            dump($mname);
//            dump($aname);
            session('node', $nodename);
//            dump(session('node.mname'));
//            dump(session('node.aname'));
//            dump(input('session.'));
//            Session::prefix('think');
//            Session::set('name','thinkphp');
//            Session::set('aname',$aname);
//            Session::set('mname',$mname,'think');

            // 查询用户,角色,节点,商品数量并存入session
            $adminusercount = 0;
            $rolecount = 0;
            $nodecount = 0;
            $goodscount = 0;
            $adminusercount = db('admin_user')
                ->count('id');
            $rolecount = db('role')
                ->count('id');
            $nodecount = db('node')
                ->count('id');
            $goodscount = db('goods')
                ->count('id');

            session('info.adminusercount',$adminusercount);
            session('info.rolecount',$rolecount);
            session('info.nodecount',$nodecount);
            session('info.goodscount',$goodscount);

            $this->success('登录成功,正在跳转页面', url('admin/Index/index'),'',1);
        }else{
            $this->error('你的帐号或密码不正确');
        }
//        dump($userinfo);
    }

    public function logout()
    {
        session(null);
        $this->redirect('admin/index/login');
    }

    public function notfound()
    {
        return view();
    }

    public function home()
    {
        return view();
    }
}
